package com.zebra.remoting.tms;

import cn.hutool.core.util.RandomUtil;
import com.zebra.common.concurrent.ZebraExecutorManager;
import com.zebra.common.concurrent.ZebraScheduler;

import java.util.concurrent.TimeUnit;

/**
 * @Auther: SK
 * @Date: 2021/8/21 22:23
 * @Description:
 */
public abstract class AbstractOnlineNumberService {

    private long onlineNumber;

    public void start(){
        ZebraScheduler scheduler = new ZebraScheduler(1);
        scheduler.scheduleWithFixedDelay(this::sendOnlineNumber, 200,  20000, TimeUnit.SECONDS, ZebraExecutorManager.getInstance().getDefaultExecutor());
    }

    private void sendOnlineNumber(){
        long newOnlineNumber = getOnlineNumber();
        if (newOnlineNumber != onlineNumber){
            onlineNumber = newOnlineNumber;
            TmsSendMessageUtil.sendOnlineNumToRA(onlineNumber);
        }
    }

    public abstract long getOnlineNumber();
}
